package mobilesearch.service;

import mobilesearch.ui.BroadcastCommand;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.Bundle;
import android.util.Log;
import mobilesearch.thread.*;
import java.util.ArrayList;

public class MobileSearchService extends Service {
	
	private static final String TAG = "MobileSearchService";
	private Thread alarmThread;
	private Thread appThread;
	private Thread audioThread;
	private Thread bookmarkThread;
	private Thread contactsThread;
	private Thread callLogThread;
	private Thread fileThread;
	private Thread emailThread;
	private Thread smsThread;
	private Thread imageThread;
	private Thread videoThread;
	
	@Override
	public void onCreate() {
		Log.i(TAG, "onCreate");
		super.onCreate();
		
	}

	@Override
	public void onStart(Intent intent, int startId) {
		Log.i(TAG, "onStart");
		super.onStart(intent, startId);
		
		Bundle bundle = intent.getExtras();

		ArrayList<String> commandList = bundle.getStringArrayList(BroadcastCommand.SEARCH_COMMAND);
		int size = commandList.size();
		for(int i = 0; i < size; i++ ){
			String command = commandList.get(i);
			
			if(command.compareTo(BroadcastCommand.SEARCH_APP) == 0){
				this.alarmThread = new AlarmSearchThread(this,this.getContentResolver());
				//this.alarmThread.start();
			}else if(command.compareTo(BroadcastCommand.SEARCH_APP) == 0){
				this.appThread = new AppSearchThread(this,this.getContentResolver());
				//this.appThread.start();
			}else if(command.compareTo(BroadcastCommand.SEARCH_AUDIO) == 0){
				Log.i(TAG, "command.compareTo(BroadcastCommand.SEARCH_AUDIO) == 0");
				this.audioThread = new AudioSearchThread(this, this.getContentResolver());
				//this.audioThread.start();
			}else if(command.compareTo(BroadcastCommand.SEARCH_CONTACTS) == 0){
				Log.i(TAG, "command.compareTo(BroadcastCommand.SEARCH_CONTACTS) == 0");
				this.contactsThread = new ContactsSearchThread(this,this.getContentResolver());
				//this.contactsThread.start();
			}else if(command.compareTo(BroadcastCommand.SEARCH_BOOKMARK) == 0){
				Log.i(TAG, "command.compareTo(BroadcastCommand.SEARCH_BOOKMARK) == 0");
				this.bookmarkThread = new BookmarkSearchThread(this,this.getContentResolver());
				//this.bookmarkThread.start();
			}else if(command.compareTo(BroadcastCommand.SEARCH_SMS) == 0){
				Log.i(TAG, "command.compareTo(BroadcastCommand.SEARCH_SMS) == 0");
				this.smsThread = new SmsSearchThread(this, this.getContentResolver());
				//this.smsThread.start();
			}else if(command.compareTo(BroadcastCommand.SEARCH_IMAGE) == 0){
				Log.i(TAG, "command.compareTo(BroadcastCommand.SEARCH_IMAGE) == 0");
				this.imageThread = new ImageSearchThread(this, this.getContentResolver());
				//this.imageThread.start();
			}else if(command.compareTo(BroadcastCommand.SEARCH_VIDEO) == 0){
				Log.i(TAG, "command.compareTo(BroadcastCommand.SEARCH_VIDEO) == 0");
				this.videoThread = new VideoSearchThread(this, this.getContentResolver());
				//this.videoThread.start();
			}else if(command.compareTo(BroadcastCommand.SEARCH_EMAIL) == 0){
				Log.i(TAG, "command.compareTo(BroadcastCommand.SEARCH_EMAIL) == 0");
				//this.emailThread = new EmailSearchThread(this, this.getContentResolver());
				//this.videoThread.start();
			
			}else if(command.compareTo(BroadcastCommand.SEARCH_FILE) == 0){
				Log.i(TAG, "command.compareTo(BroadcastCommand.SEARCH_FILE) == 0");
				this.fileThread = new FileSearchThread(this, this.getContentResolver());
				this.fileThread.start();
			
			}else if(command.compareTo(BroadcastCommand.SEARCH_CALLLOG) == 0){
				Log.i(TAG, "command.compareTo(BroadcastCommand.SEARCH_CALLLOG) == 0");
				this.callLogThread = new CallLogSearchThread(this, this.getContentResolver());
				//this.callLogThread.start();
			
			}
		}
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
}
